<template>
  <div style="height:100%;position:relative;">
    <div :id="id" style="height: 100%" :style="{visibility:data&&data.length>0?'visible':'hidden'}"></div>
    <div v-show="!data||data.length===0" class="no-data">暂无数据</div>
  </div>
</template>

<script>
import * as echarts from "echarts";
import { hexToRgba } from "../../../util/tools"

export default {
  name: "bar",
  props: {
    id: {
      type: String,
      default: "bar",
    },
    color: {
      type: Array,
      default: () => [],
    },
    data: {
      type: Array,
      default: () => [],
    },
    isSeparate: {
      type: Boolean,
      default: true
    },
    grid: {
      type: Object,
      default() {
        return {}
      },
    }
  },
  watch: {
    data: {
      deep: true,
      handler(nVal) {
        if (this.chart) {
          this.setOption();
        } else {
          this.initChart();
        }
      },
    },
  },
  data() {
    return {
      chart: null,
      selfGrid: {
        left: 0,
        right: 0,
        bottom: 0,
        top: 25,
        containLabel: true,
      }
    };
  },
  mounted() {
    let grid = JSON.stringify(Object.assign(this.selfGrid, this.grid))
    this.selfGrid = JSON.parse(grid)
    this.initChart();
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id), "macarons");
      this.setOption();

      this.chart.on("click", (param) => {
        this.$emit("onClick", this.data[param.dataIndex].key);
      });

      window.addEventListener("resize", () => {
        this.chart && this.chart.resize();
      });
    },
    setOption() {
      if (!this.data || Object.keys(this.data).length === 0) {
        return;
      }

      let xData = this.data.map((item) => {
        return item.name;
      });
      let yData = this.data.map((item) => {
        return item.count;
      });

      let option = {
        tooltip: {
          trigger: "axis",
          borderWidth: 0,
          backgroundColor: "rgba(50, 50, 50, 0.7)",
          textStyle: { color: "#ffffff" },
          axisPointer: {
            type: "shadow",
          },
        },
        grid: this.selfGrid,
        xAxis: {
          type: "category",
          boundaryGap: true,
          axisTick: { show: false },
          axisLine: { show: false },
          axisLabel: {
            color: "#689FF6",
            interval: 0,
          },
          splitLine: { show: false },
          data: xData,
        },
        yAxis: {
          offset: 0,
          type: "value",
          splitNumber: 3,
          minInterval: 1,
          axisLine: { show: false },
          axisTick: { show: false },
          axisLabel: {
            color: "#689FF6",
          },
          splitLine: {
            lineStyle: {
              color: "rgba(235,236,240,0.1)",
            },
          },
        },
        series: [
          {
            type: "bar",
            barWidth: "40%",
            label: {
              show: true,
              position: "top",
              color: "#fff",
              fontSize: 10,
            },
            itemStyle: {
              // color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              //   { offset: 0, color: this.color[0] },
              //   { offset: 1, color: this.color[1] },
              // ]),
              color: this.isSeparate ? new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    { offset: 0, color: this.color[0] },
                    { offset: 1, color: this.color[1] },
                ]) : (params) => {
                  if (params.dataIndex % 2 == 0) {
                    return new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 0, color: this.color[0] },
                        { offset: 1, color: hexToRgba(this.color[0], 0) },
                    ])
                  } else {
                    return new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 0, color: this.color[1] },
                        { offset: 1, color: hexToRgba(this.color[1], 0) },
                    ])
                  }
                },
            },
            data: yData,
          },
        ],
      };

      this.chart.setOption(option);
    },
  },
};
</script>

<style lang="less" scoped>
.no-data {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #c5c8ce;
}
</style>